package icbc.app.control
{
	import fl.containers.ScrollPane;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class SegList extends Sprite
	{
		private static const ITEM_GAP_X:uint = 15;
		private static const ITEM_GAP_Y:uint = 3;
		private static const ITEM_Y:uint = 0;
		private static const ITEM_X:uint = 4;
		private var data:Array;
		private var place:Sprite;
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var itemHeight:uint = 40;
		private var segpane:ScrollPane;
		private var nextItemPosy:uint; 
		
		public function SegList(vWidth:uint, vHeight:uint)
		{
			super();
			data = [];
			controlWidth = vWidth;
			controlHeight = vHeight;
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
		
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			initContent();
		}
		
		private function initContent():void
		{
			segpane = new ScrollPane();
			segpane.setSize(controlWidth, controlHeight);
			segpane.horizontalScrollPolicy = "off";
			var newSkinClip:Sprite = new Sprite();
			newSkinClip.graphics.clear();
			newSkinClip.graphics.beginFill(0xff0000, 0);
			newSkinClip.graphics.drawRect(0,0,272.0, 191.0);
			newSkinClip.graphics.endFill();
			segpane.setStyle("skin", newSkinClip );
			segpane.setStyle("upSkin", newSkinClip );
			segpane.setStyle("borderColor", 0xFFFFFF);
			segpane.setStyle("borderStyle","none");
			segpane.drawFocus(false);
			segpane.focusRect = null;
			segpane.tabEnabled = false;
			addChild(segpane);
			
			place = new Sprite();
			segpane.source = place;
			nextItemPosy = ITEM_Y;
		}
		
		public function addItem(item:Object):void
		{
			data.push(item);
			buildItem(item);
		}
		
		private function buildItem(listItem:Object):void
		{
			var item:SegListItem = new SegListItem(listItem, controlWidth-ITEM_GAP_X, itemHeight);
			item.x = ITEM_X;
			item.y = nextItemPosy;
			place.addChild(item);
			nextItemPosy = ITEM_Y + place.height + ITEM_GAP_Y;
			segpane.refreshPane();
			segpane.invalidate();
		}
		
		public function removeAll():void
		{
			data = [];
			place.removeChildren();
			nextItemPosy = ITEM_Y;
		}
	}
}